package com.hqyj.service.impl;

import com.hqyj.pojo.MyNode;
import com.hqyj.pojo.Power;
import com.hqyj.dao.PowerDao;
import com.hqyj.pojo.R;
import com.hqyj.service.PowerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-11-03 09:36:00
 */
@Service
public class PowerServiceImpl extends ServiceImpl<PowerDao, Power> implements PowerService {

    @Resource
    PowerDao powerDao;
    @Override
    public R queryPowewrByName(String userName) {
        //查询用户的权限信息
        List<Power> list = powerDao.queryPowerByName(userName);
        //创建树形节点集合对象
        List<MyNode> nodeList = new LinkedList<>();

        //构建树形节点对象集合
        if(list.size()>0){
            for(Power p :list){
               //查询一级菜单
               if(p.getParentId()==0){
                   MyNode node = new MyNode();
                   node.setName(p.getPowerName());
                   node.setIndex(p.getPowerIndex());
                   //创建子菜单集合对象
                   List<MyNode> childrenList = new LinkedList<>();
                   //查询一级菜单的子菜单
                   for(Power pp:list){
                       if(pp.getParentId()==p.getNodeId()){
                           //构建子菜单对象
                           MyNode childNode = new MyNode();
                           childNode.setName(pp.getPowerName());
                           childNode.setUrl(pp.getPowerUrl());
                           childrenList.add(childNode);
                       }
                   }
                   //添加子菜单
                   node.setChildren(childrenList);
                   //添加到节点集合里
                   nodeList.add(node);
               }
            }
            return R.ok(nodeList);
        }

        return R.error();
    }
}
